﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Customer_BaseInfo。
    /// </summary>
    public class Customer_BaseInfo
    {
        private const string CONNECTSTRING_SEARCH = "Chenxin_House_Search";
        private const string CONNECTSTRING = "Chenxin_House";

        public Customer_BaseInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid CustomerId)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer_BaseInfo where CustomerId=@CustomerId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, CustomerId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Customer_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Customer_BaseInfo(");
            strSql.Append("CityId,CityName,CustomerId,CustomerCode,CustomerName,Address,Mobile,Phone,Nationality,Sex,CertificateType,CertificateNo,DemandType,CustomerType,Status,CustomerKind,CompanyId,CompanyName,Adddate,AddPersonId,AddPerson,AddDepartId,AddDepart,OprPersonId,OprPerson,OprDepartId,OprDepart,LastTraceDay,PubLevel)");

            strSql.Append(" values (");
            strSql.Append("@CityId,@CityName,@CustomerId,@CustomerCode,@CustomerName,@Address,@Mobile,@Phone,@Nationality,@Sex,@CertificateType,@CertificateNo,@DemandType,@CustomerType,@Status,@CustomerKind,@CompanyId,@CompanyName,@Adddate,@AddPersonId,@AddPerson,@AddDepartId,@AddDepart,@OprPersonId,@OprPerson,@OprDepartId,@OprDepart,@LastTraceDay,@PubLevel)");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerCode", DbType.AnsiString, model.CustomerCode);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, model.Phone);
            db.AddInParameter(dbCommand, "Nationality", DbType.AnsiString, model.Nationality);
            db.AddInParameter(dbCommand, "Sex", DbType.AnsiString, model.Sex);
            db.AddInParameter(dbCommand, "CertificateType", DbType.AnsiString, model.CertificateType);
            db.AddInParameter(dbCommand, "CertificateNo", DbType.AnsiString, model.CertificateNo);
            db.AddInParameter(dbCommand, "DemandType", DbType.Int32, model.DemandType);
            db.AddInParameter(dbCommand, "CustomerType", DbType.AnsiString, model.CustomerType);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CustomerKind", DbType.Int32, model.CustomerKind);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Adddate", DbType.DateTime, model.Adddate);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "OprPersonId", DbType.Guid, model.OprPersonId);
            db.AddInParameter(dbCommand, "OprPerson", DbType.AnsiString, model.OprPerson);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "LastTraceDay", DbType.DateTime, model.LastTraceDay);
            db.AddInParameter(dbCommand, "PubLevel", DbType.Int32, model.PubLevel);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Customer_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Customer_BaseInfo set ");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CustomerCode=@CustomerCode,");
            strSql.Append("CustomerName=@CustomerName,");
            strSql.Append("Address=@Address,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Nationality=@Nationality,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("CertificateType=@CertificateType,");
            strSql.Append("CertificateNo=@CertificateNo,");
            strSql.Append("DemandType=@DemandType,");
            strSql.Append("CustomerType=@CustomerType,");
            strSql.Append("Status=@Status,");
            strSql.Append("CustomerKind=@CustomerKind,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("Adddate=@Adddate,");
            strSql.Append("AddPersonId=@AddPersonId,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddDepartId=@AddDepartId,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("OprPersonId=@OprPersonId,");
            strSql.Append("OprPerson=@OprPerson,");
            strSql.Append("OprDepartId=@OprDepartId,");
            strSql.Append("OprDepart=@OprDepart,");
            strSql.Append("LastTraceDay=@LastTraceDay,");
            strSql.Append("PubLevel=@PubLevel");
            strSql.Append(" where CustomerId=@CustomerId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerCode", DbType.AnsiString, model.CustomerCode);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, model.Phone);
            db.AddInParameter(dbCommand, "Nationality", DbType.AnsiString, model.Nationality);
            db.AddInParameter(dbCommand, "Sex", DbType.AnsiString, model.Sex);
            db.AddInParameter(dbCommand, "CertificateType", DbType.AnsiString, model.CertificateType);
            db.AddInParameter(dbCommand, "CertificateNo", DbType.AnsiString, model.CertificateNo);
            db.AddInParameter(dbCommand, "DemandType", DbType.Int32, model.DemandType);
            db.AddInParameter(dbCommand, "CustomerType", DbType.AnsiString, model.CustomerType);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CustomerKind", DbType.Int32, model.CustomerKind);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Adddate", DbType.DateTime, model.Adddate);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "OprPersonId", DbType.Guid, model.OprPersonId);
            db.AddInParameter(dbCommand, "OprPerson", DbType.AnsiString, model.OprPerson);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "LastTraceDay", DbType.DateTime, model.LastTraceDay);
            db.AddInParameter(dbCommand, "PubLevel", DbType.Int32, model.PubLevel);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid CustomerId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer_BaseInfo ");
            strSql.Append(" where CustomerId=@CustomerId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, CustomerId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_BaseInfo GetModel(Guid CustomerId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CityId,CityName,CustomerId,CustomerCode,CustomerName,Address,Mobile,Phone,Nationality,Sex,CertificateType,CertificateNo,DemandType,CustomerType,Status,CustomerKind,CompanyId,CompanyName,Adddate,AddPersonId,AddPerson,AddDepartId,AddDepart,OprPersonId,OprPerson,OprDepartId,OprDepart,LastTraceDay,PubLevel from Customer_BaseInfo ");
            strSql.Append(" where CustomerId=@CustomerId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, CustomerId);
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustomerId,CustomerCode,CustomerName,Address,Mobile,Phone,Nationality,Sex,CertificateType,CertificateNo,DemandType,CustomerType,Status,CustomerKind,CompanyId,CompanyName,Adddate,AddPersonId,AddPerson,AddDepartId,AddDepart,OprPersonId,OprPerson,OprDepartId,OprDepart,LastTraceDay,PubLevel ");
            strSql.Append(" FROM Customer_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">每页显示记录数</param>
        /// <param name="OrderField">排序字段</param>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="FieldList">查询字段</param>
        /// <param name="TableFieldList"></param>
        /// <param name="DoCount">是否需要总记录数</param>
        /// <returns></returns>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, "Customer_BaseInfo");
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Customer_BaseInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Customer_BaseInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustomerId,CustomerCode,CustomerName,Address,Mobile,Phone,Nationality,Sex,CertificateType,CertificateNo,DemandType,CustomerType,Status,CustomerKind,CompanyId,CompanyName,Adddate,AddPersonId,AddPerson,AddDepartId,AddDepart,OprPersonId,OprPerson,OprDepartId,OprDepart,LastTraceDay,PubLevel ");
            strSql.Append(" FROM Customer_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Customer_BaseInfo> list = new List<Chenxin.Lib.House.Entity.Customer_BaseInfo>();
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_BaseInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = new Chenxin.Lib.House.Entity.Customer_BaseInfo();
            object ojb;
            ojb = dataReader["CustomerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CustomerId = new Guid(ojb.ToString());
            }
            model.CustomerCode = dataReader["CustomerCode"].ToString();
            model.CustomerName = dataReader["CustomerName"].ToString();
            model.Address = dataReader["Address"].ToString();
            model.Mobile = dataReader["Mobile"].ToString();
            model.Phone = dataReader["Phone"].ToString();
            model.Nationality = dataReader["Nationality"].ToString();
            model.Sex = dataReader["Sex"].ToString();
            model.CertificateType = dataReader["CertificateType"].ToString();
            model.CertificateNo = dataReader["CertificateNo"].ToString();
            ojb = dataReader["DemandType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DemandType = (int)ojb;
            }
            model.CustomerType = dataReader["CustomerType"].ToString();
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            ojb = dataReader["CustomerKind"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CustomerKind = (int)ojb;
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["Adddate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Adddate = (DateTime)ojb;
            }
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            ojb = dataReader["AddDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddDepartId = new Guid(ojb.ToString());
            }
            model.AddDepart = dataReader["AddDepart"].ToString();
            ojb = dataReader["OprPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprPersonId = new Guid(ojb.ToString());
            }
            model.OprPerson = dataReader["OprPerson"].ToString();
            ojb = dataReader["OprDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprDepartId = new Guid(ojb.ToString());
            }
            model.OprDepart = dataReader["OprDepart"].ToString();
            ojb = dataReader["LastTraceDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LastTraceDay = (DateTime)ojb;
            }
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["PubLevel"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PubLevel = (int)ojb;
            }
            return model;
        }

        //*******************************************
        //功能概要       ：获取房源Code
        //参数説明       ：
        //返回值説明     ：
        //功能詳細       ：获取客源Code
        //作成者         ：姚志强
        //作成日         ：2010-03-01
        //修改者         ：
        //修改日         ：
        //*******************************************
        public static string GetCustomerCode(Guid userId)
        {

            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetStoredProcCommand("usp_AutoGenerateHouseCode");

            //System.Data.SqlClient.SqlParameter 
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, userId);
            db.AddInParameter(dbCommand, "CodeType", DbType.Int32, "20");
            db.AddOutParameter(dbCommand, "HouseCode", DbType.AnsiString, 150);
            db.ExecuteNonQuery(dbCommand);

            return db.GetParameterValue(dbCommand, "HouseCode").ToString();
        }

        //*******************************************
        //功能概要       ：获取当前用户所属客户数量
        //参数説明       ：OprPersonId：客户Id
        //返回值説明     ：当前用户所属客户数量
        //功能詳細       ：获取当前用户所属客户数量
        //作成者         ：何基锋
        //作成日         ：2010-06-23
        //*******************************************
        public static int GetCntOfCustomer(Guid OprPersonId)
        {
            int iRet = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select COUNT(1) ");
            strSql.Append(" FROM Customer_BaseInfo ");
            strSql.Append(" Where CustomerKind = 20 ");
            strSql.Append(" And PubLevel = 0 ");
            strSql.AppendFormat(" And OprPersonId = '{0}' ",OprPersonId);

            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DataSet ds = db.ExecuteDataSet(CommandType.Text, strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                iRet = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }

            return iRet;
        }
        #endregion  成员方法
    }
}

